//
//  MyTableViewController.m
//  LimitFree
//
//  Created by qianfeng on 15/8/27.
//  Copyright (c) 2015年 Hailong.wang. All rights reserved.
//

#import "MyTableViewController.h"
#import "BaseViewController.h"
#import "NetRequestManager.h"
#import "NetInterface.h"
#import "ThemMedol.h"
#import "Subject.h"
#import "LoadingView.h"
#import "LeftViewController.h"
#import "SettingViewController.h"


@interface MyTableViewController ()
{
       NSMutableArray *_dataArr;
       NSString* _number;
    //分类页面
    
    //分类页面
    LeftViewController *_lvc;
}
@property(nonatomic,assign)NSInteger  page;
@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [LoadingView showLoadingView:self.view];
    self.title=@"专题";
    
    
    _lvc = [LeftViewController shareSingleton];
    [self createView];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)createView{
    //UIButton *leftBtn = [Factory createButtonWithTitle:@"分类" frame:CGRectMake(0, 5, 40, 30) titleFont:12.f textColor:[UIColor blackColor] backgroundColor:RGB(200, 200, 200) target:self selector:@selector(toShowAppClassify)];
    //[self.navigationController addSubview:leftBtn];
    UIBarButtonItem* leftBtn=[[UIBarButtonItem alloc]initWithTitle:@"分类" style:UIBarButtonItemStylePlain target:self action:@selector(toShowAppClassify)];
    
     self.navigationItem.leftBarButtonItem = leftBtn;
    UIBarButtonItem* right=[[UIBarButtonItem alloc]initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(toAppShowSetting)];
    self.navigationItem.rightBarButtonItem = right;
    
}
//将分类展示的行为放在子视图控制器
-(void)toShowAppClassify{
    [_lvc showView:^(NSString* category){
        //NSLog(@"%@",category);
        
        //666 通过id 分类
        //_categoreID=category;
        //[self resetParame];
        //[self getNetData];
    }];
}
- (void)toAppShowSetting {
    SettingViewController* set=[[SettingViewController alloc]init];
    //隐藏底部导航
    set.hidesBottomBarWhenPushed=YES;
    
    [self.navigationController pushViewController:set animated:YES];
}
-(void)initData{
     _dataArr=[[NSMutableArray alloc]init];
    self.page=1;
    //__weak typeof (self) weakSelf=self;
    [NetRequestManager GET:kSubjectUrl parame:[NSDictionary dictionaryWithObjectsAndKeys:_number,@"number",[NSString stringWithFormat:@"%ld",self.page] ,@"page",nil] succes:^(AFHTTPRequestOperation *operation, id responseObject) {
        //  NSLog(@"%@",operation.request);
        
        //weakSelf.dataTwo=[[ThemMedol alloc]initWithData:responseObject error:nil];
        
        //NSLog(@"temp=%@",weakSelf.dataTwo);
        
        NSArray* arry=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"%@",arry);
        for (NSDictionary *dic in arry) {
            
            
            ThemMedol* them=[[ThemMedol alloc]init];
            them.title=[dic objectForKey:@"title"];
            them.desc_img=[dic objectForKey:@"img"];
            NSArray* applications=[dic objectForKey:@"applications"];
            for (NSDictionary *dict in applications) {
                them.name=[dict objectForKey:@"name"];
                them.downloads=[dict objectForKey:@"downloads"];
                them.iconUrl=[dict objectForKey:@"iconUrl"];
                them.ratingOverall=[dict objectForKey:@"ratingOverall"];
                [_dataArr addObject:them];
                NSLog(@"%@",them);
            }
        }
        
        [self.tableView reloadData];
        
        [LoadingView hideLoadingView];
        //刷新页面
       // [weakSelf refreshView];
        
        //
        //[LoadingView hideLoadingView];
        //
        // NSLog(@"%@ %@",operation.request ,responseObject);
    } failed:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
//    // Return the number of sections.
//    return 0;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return _dataArr.count;
}

//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellName = @"SubjectCell";
    //复用队列获取 空闲的cell 如果没有 那么 创建新的
    Subject *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell==nil) {
        cell=[[[NSBundle mainBundle]loadNibNamed:@"Subject" owner:self options:nil]lastObject];
    }
    ThemMedol *model = [_dataArr objectAtIndex:indexPath.row];
    cell.nameLabel.text=model.name;
    [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.iconUrl]];
    cell.applicationname.text=model.title;
    cell.down.text=model.downloads;
    cell.ratingOverall.text=model.ratingOverall;
    [cell.imgImageView sd_setImageWithURL:[NSURL URLWithString:model.desc_img]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 375;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
